// components/tab-switch/index.js
Component({
  options: {
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   */
  properties: {
    tabList: {
      type: Array,
      value: [
        {
          key: 'key',
          name: 'name'
        }
      ]
    },

    scroll: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    curIndex: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onTabSwitch(event) {
      console.log(22)
      const { index } = event.target.dataset;
      this.setData({
        curIndex: index
      });
    },

    handleToScroll(event) {
      const { scrollLeft } = event.detail;
      const { curIndex } = this.data;
      const query = this.createSelectorQuery();
      const ref = query.select('.tab-content__item');
      ref.boundingClientRect((res) => {
        const { width } = res;
        const index = Math.round(scrollLeft / width);
        if (index !== curIndex) {
          this.setData({
            curIndex: Math.round(scrollLeft / width)
          });
        }
      }).exec();
    }
  }
})